﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;

namespace XiaoweiVoiceAI.ResponseCreators
{
    internal static class MusicResponseCreator
    {
        private class MusicResponseData : IPlayControlResponseData
        {
            public PlayControlType PlayControlType { get; set; }

            public PlayerType PlayerType => PlayerType.Music;

            public IPlayableResource PlayableResource { get; set; }

            public string PlayTittle { get; set; }

            public object PlayStateAdditionalMessage {get; set;}

            public ResponseType ResponseType => ResponseType.PlayControl;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }
            public string ResponseText { get; set; }
            public string TTSUrl { get; set; }

            public bool RestoreInHistory {get; set;}

            public object DataContext { get; set; }

            public int? ControlValue { get; set; }
        }
        private class MusicResource : IMusicResource
        {
            public IReadOnlyList<string> PlayIdList { get; set; }
        }

        private static Dictionary<XiaoweiV2.CommandId, PlayControlType> commandIdMap = new Dictionary<XiaoweiV2.CommandId, PlayControlType>()
        {
            {XiaoweiV2.CommandId.Play, PlayControlType.Play},
            {XiaoweiV2.CommandId.Stop, PlayControlType.Stop},
            {XiaoweiV2.CommandId.Pause, PlayControlType.Pause},
            {XiaoweiV2.CommandId.Prev, PlayControlType.Prev},
            {XiaoweiV2.CommandId.Next, PlayControlType.Next},
            {XiaoweiV2.CommandId.AddOffset, PlayControlType.AddOffset},
            {XiaoweiV2.CommandId.SetOffsetTo, PlayControlType.SetOffsetTo},
            {XiaoweiV2.CommandId.PlayMode, PlayControlType.PlayMode},
        };

        private static Dictionary<XiaoweiV2.CommandId, string> responseTextMap = new Dictionary<XiaoweiV2.CommandId, string>()
        {
            {XiaoweiV2.CommandId.Play,"好的，开始播放" },
            {XiaoweiV2.CommandId.Stop,"好的，停止播放" },
            {XiaoweiV2.CommandId.Pause,"好的，暂停播放" },
            {XiaoweiV2.CommandId.Prev,"好的，开始播放上一首" },
            {XiaoweiV2.CommandId.Next,"好的，开始播放下一首" },
            {XiaoweiV2.CommandId.AddOffset,"好的，已重新定位" },
            {XiaoweiV2.CommandId.SetOffsetTo,"好的，已重新定位" },
            {XiaoweiV2.CommandId.PlayMode,"好的，已切换播放模式" }
        };

        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, XiaoweiV2.ResponseParam param)
        {
            MusicResponseData musicResponseData = new MusicResponseData();
            musicResponseData.RequestText = param.RequestText;
            musicResponseData.ResponseText = param.ResponseText;
            musicResponseData.TTSUrl = XiaoweiV2.ResponseHelper.GetTTSUrl(param);
            musicResponseData.RestoreInHistory = !string.IsNullOrEmpty(param.RequestText);
            if (XiaoweiV2.ResponseHelper.IsCommand(param))
            {
                if(commandIdMap.ContainsKey(param.ControlId))
                {
                    musicResponseData.PlayControlType = commandIdMap[param.ControlId];
                    if(int.TryParse(param.ControlValue,out int value))
                    {
                        if (param.ControlId == XiaoweiV2.CommandId.PlayMode)
                        {
                            switch ((XiaoweiV2.PlayMode)value)
                            {
                                case XiaoweiV2.PlayMode.SequentialNoLoop:
                                    musicResponseData.ControlValue = (int)PlayMode.OrderPlay;
                                    break;
                                case XiaoweiV2.PlayMode.SequentialLoopList:
                                    musicResponseData.ControlValue = (int)PlayMode.LoopPlay;
                                    break;
                                case XiaoweiV2.PlayMode.ShuffleNoLoop:
                                    break;
                                case XiaoweiV2.PlayMode.ShuffleLoop:
                                    musicResponseData.ControlValue = (int)PlayMode.RandomPlay;
                                    break;
                                case XiaoweiV2.PlayMode.SingleLoop:
                                    musicResponseData.ControlValue = (int)PlayMode.SinglePlay;
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            musicResponseData.ControlValue = value / 1000;
                        }
                    }
                    else
                    {
                        musicResponseData.ControlValue = null;
                    }
                    if (!string.IsNullOrEmpty(param.RequestText))
                    {
                        musicResponseData.ResponseText = responseTextMap[param.ControlId];
                        musicResponseData.TTSUrl = xiaoweiVoiceAI.TextToVoice(musicResponseData.ResponseText);
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                if (!xiaoweiVoiceAI.HasPlayable) return null;
                //if there is no song list, don't trigger qqlive to play
                musicResponseData.PlayControlType = PlayControlType.PlayResource;
                List<string> playIds = new List<string>();
                foreach (var item in param.ResourceGroups)
                {
                    try
                    {
                        playIds.Add(item.Resources.Last().Id.Split("&")[0].Split("=")[1]);
                    }
                    catch (Exception)
                    {
                        return null;
                    }
                }
                musicResponseData.PlayableResource = new MusicResource() { PlayIdList = playIds };
            }
            return musicResponseData;
        }
    }
}
